﻿/*
 * Created by SharpDevelop.
 * User: CrOnO
 * Date: 15/11/2011
 * Time: 07:20 a.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data;
using System.Data.SqlClient;
using Business_Entity;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

namespace DAOFactory
{
	/// <summary>
	/// Description of DAOAsistenciaDiaria.
	/// </summary>
	public class DAOAsistenciaDiaria
	{
		public DAOAsistenciaDiaria()
		{
		}
		
		public List<string[]> buscarAsistencia(BEEmpleado empleado, DateTime fechaI, DateTime fechaF)
		{
			try
			{
				SqlCommand cmd = new SqlCommand("RRHH_BUSCAR_ASISTENCIA_L", ConnectionManager.getConnection());
				cmd.CommandType = CommandType.StoredProcedure;
				
				cmd.Parameters.Add("@IDEMPLEADO", SqlDbType.Int);
				cmd.Parameters["@IDEMPLEADO"].Value = empleado.IdEmpleado;
				cmd.Parameters.Add("@FECHAINICIO", SqlDbType.DateTime);
				cmd.Parameters["@FECHAINICIO"].Value = fechaI;
				cmd.Parameters.Add("@FECHAFIN", SqlDbType.DateTime);
				cmd.Parameters["@FECHAFIN"].Value = fechaF;
				
				SqlDataReader amd = cmd.ExecuteReader();
				
				List<string[]> lista = new List<string[]>();
				
				if (amd.HasRows) 
				{
					while(amd.Read())
					{
						try
						{
							String horaentrada = "";
							String horasalida = "";
							
						    if (amd[6] == DBNull.Value)
							{
								horaentrada = "-";
							}
						    else
						    	horaentrada = DateTime.Parse(amd[6].ToString()).ToShortTimeString();
						    
							if (amd[7] == DBNull.Value)
							{
								horasalida = "-";
							}
							else
								horasalida = DateTime.Parse(amd[7].ToString()).ToShortTimeString();
							string[] fila = null;
							fila = new string[]{
									amd[0].ToString(),
									amd[1].ToString(),
									amd[2].ToString(),
									amd[3].ToString(),
									amd[4].ToString(),
									DateTime.Parse(amd[5].ToString()).ToShortDateString(),
									horaentrada,
									horasalida
						            };
							lista.Add(fila);
						}
						catch
						{
//							System.Windows.Forms.MessageBox.Show("Error al buscar actividades","Error");
							amd.Close();
							return null;						
						}
					}
					amd.Close();
					return lista;
				}
				else
				{
					amd.Close();
					return null;
				}
			}
			catch
			{
//				System.Windows.Forms.MessageBox.Show("Error al buscar actividades","Error");
				return null;	
			}
		}
	}
}
